﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.SessionState;
using Youz.Common.BLL;
using Youz.Common.Helper;
using Youz.Common.Entity;


namespace Youz.CreditRepay.Manage.Ajax
{
    /// <summary>
    /// ResetPwd 的摘要说明
    /// </summary>
    public class ResetPwd : IHttpHandler, IRequiresSessionState
    {

        public void ProcessRequest(HttpContext context)
        {
            context.Response.ContentType = "text/plain";
            string type = context.Request.QueryString["type"];
            string result = "";
            switch (type)
            {
                case "reset":
                    result = ReSetPwd(context);
                    break;
                case "repwd":
                    result = ResetPassword(context);
                    break;
            }


            context.Response.Write(result);
            context.Response.End();
        }

        //重置密码
        private string ResetPassword(HttpContext context)
        {
            string _newpwd = WebHelper.Post("newpwd").Trim();
            string _newpwdok = WebHelper.Post("newpwdok").Trim();

            if (String.IsNullOrEmpty(_newpwd)) return Utility.AjaxResult(false, Lang_LanguageBLL.LanguageByWebSite(LanguageModule.M_Other, "SetPwdError10", "请输入您新的登录密码"));
            if (String.IsNullOrEmpty(_newpwdok)) return Utility.AjaxResult(false, Lang_LanguageBLL.LanguageByWebSite(LanguageModule.M_Other, "SetPwdError11", "请再次输入您新的密码"));
            if (!_newpwd.Equals(_newpwdok)) return Utility.AjaxResult(false, Lang_LanguageBLL.LanguageByWebSite(LanguageModule.M_Other, "SetPwdError12", "您二次输入的密码不一致，请重新输入"));


            //验证Email是否正确
            if (context.Session["ResetPwd_MemberAccount"] != null)
            {
                string _memberAccount = DESEncrypt.Decrypt(context.Session["ResetPwd_MemberAccount"].ToString());
                Sys_AdminInfoBLL bll = new Sys_AdminInfoBLL();
                if (!bll.Exists(_memberAccount))
                {
                    Utility.WriteErrorLog("管理员找回密码", "设置新密码", "要找回密码的賬號不正確,帐号为:" + _memberAccount);
                    return Utility.AjaxResult(false, Lang_LanguageBLL.LanguageByWebSite(LanguageModule.M_Other, "SetPwdError13", "要重置的賬號不正確，請重新設置您的登錄密碼"));
                }

                if (bll.ResetPwd(_memberAccount, CommonHelper.GetSHA512(_newpwd), CommonHelper.GetSHA512(_newpwd)))
                {
                    return Utility.AjaxResult(true, "");
                }
                else
                {
                    return Utility.AjaxResult(false, Lang_LanguageBLL.LanguageByWebSite(LanguageModule.M_Other, "SetPwdError14", "重置密碼失敗，請稍後再試"));
                }

            }
            else
            {
                return Utility.AjaxResult(false, Lang_LanguageBLL.LanguageByWebSite(LanguageModule.M_Other, "SetPwdError15", "重置密碼失效，請重新重置密碼"));
            }
        }

        private string ReSetPwd(HttpContext context)
        {
            string MemberAccount = WebHelper.Post("MAccount").Trim();
            string MemberEmail = WebHelper.Post("MPassword").Trim();
            string ValidateCode = WebHelper.Post("ValidateCode").Trim();

            if (string.IsNullOrEmpty(MemberAccount)) return Utility.AjaxResult(false, Lang_LanguageBLL.LanguageByManage(LanguageModule.M_Other, "FP_account_name", "请输入您要找回密码的帐号！"));
            if (string.IsNullOrEmpty(MemberEmail)) return Utility.AjaxResult(false, Lang_LanguageBLL.LanguageByManage(LanguageModule.M_Other, "FP_Email_empt", "请输入您要找回密码帐号对应的Email！"));
            if (string.IsNullOrEmpty(ValidateCode)) return Utility.AjaxResult(false, Lang_LanguageBLL.LanguageByManage(LanguageModule.M_Other, "FP_Email_format", "Email格式错误！"));
            if (ValidateCode.ToLower() != Convert.ToString(SessionHelper.Get("ValidateCode")).ToLower())
            {
                return Utility.AjaxResult(false, Lang_LanguageBLL.LanguageByManage(LanguageModule.M_Other, "FP_ValidateCode_Erroe", "您输入的验证码不正确，请输入正确的验证码！"));
            }

            //验证帐号和邮件地址是否正确
            Sys_AdminInfoBLL bll = new Sys_AdminInfoBLL();

            if (!bll.ExistsByUserNameAndEmail(MemberAccount, MemberEmail))
            {
                Utility.WriteOperationLog("管理员找回密码", "验证帐号和Email", "帐号：" + MemberAccount + "或Email：" + MemberEmail + " 不存在！");
                return Utility.AjaxResult(false, Lang_LanguageBLL.LanguageByManage(LanguageModule.M_Other, "FP_AccountANDEmailNoExist", "您的帐号或Email不存在，请输入正确的帐号和Email！"));
            }

            SessionHelper.Set("FP_Email", MemberEmail);
            SessionHelper.Set("FP_Username", MemberAccount);

            string Result = EmailBLL.SendResetPasswordEmail(MemberAccount, MemberEmail, ConfigParameters.SysTime.Ticks.ToString());

            if (string.IsNullOrEmpty(Result))
            {
                Utility.WriteOperationLog("管理员找回密码", "发送找回密码邮件", "成功发送密码找回邮件，帐号：" + MemberAccount + "Email：" + MemberEmail);
                return Utility.AjaxResult(true, "");
            }
            else
            {
                Utility.WriteOperationLog("管理员找回密码", "发送找回密码邮件", "【失败】发送密码找回邮件，帐号：" + MemberAccount + "Email：" + MemberEmail + "，原因：" + Result);
                return Utility.AjaxResult(false, Lang_LanguageBLL.LanguageByManage(LanguageModule.M_Other, "FP_SendEmail_Failer", "发送邮件失败，请稍后重试或者联系我们！"));
            }

        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}